package service;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQService {

	private static ActiveMQConnectionFactory connectionFactory = null;
	public static Connection connection = null;
	public static Session session = null;
	public static Destination destination = null;
	private static boolean transacted = false;	

	public static void start(String watQueue) throws JMSException {
		if (connection == null) {
			connectionFactory = new ActiveMQConnectionFactory(
					ActiveMQConnection.DEFAULT_USER,
					ActiveMQConnection.DEFAULT_PASSWORD,
					"tcp://o2tv.vn:61616");
			connection = connectionFactory.createConnection();
			connection.start();
			session = connection.createSession(transacted,
					Session.AUTO_ACKNOWLEDGE);
		}
		destination = session.createQueue(watQueue);		
	}

	public static void sendImageSMS(String str) throws JMSException {
		start("imageSMS");
		MessageProducer producer = session.createProducer(destination);
		producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		TextMessage message = session.createTextMessage(str);
		producer.send(message);
	}
	
	public static void sendContentSMS(String str) throws JMSException {
		start("contentSMS");
		MessageProducer producer = session.createProducer(destination);
		producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		TextMessage message = session.createTextMessage(str);
		producer.send(message);
	}
	
	public static void sendTagSMS(String str) throws JMSException {
		start("tagSMS");
		MessageProducer producer = session.createProducer(destination);
		producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		TextMessage message = session.createTextMessage(str);
		producer.send(message);
	}

	public static void stop() {
		try {
			if (connection != null) {
				connection.close();
				connection = null;
			}
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
